package com.timous.链表;

/**
 * https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
 */

/**
 * @author Timous
 * @create 2020-12-21 15:29
 **/
public class _剑指Offer18_删除链表的节点 {

    /**
     * 从val开始，把后面的所有覆盖前面的所有元素
     * @param head
     * @param val
     * @return
     */
    public ListNode deleteNode(ListNode head, int val) {
        if (head.val == val){
            head = head.next;
        }else {
            ListNode node = getNode(head , val);
            node.next = node.next.next;
        }
        return head;
    }

    private ListNode getNode(ListNode head, int val) {
        ListNode node = head;
        while (node.next != null){
            if (node.next.val == val){
                return node;
            }
            node = node.next;
        }
        return null;
    }


}
